package com.example.demo.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.mapper.ScoreMapper;
import com.example.demo.entity.Message;
import com.example.demo.entity.Score;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
public class GetScoreByFlagController {
    @Autowired
    private ScoreMapper sm;
    @RequestMapping("/getscorebyflag")
    public void getScoreByFlag(HttpServletRequest request, HttpServletResponse response) throws IOException {
            String idStr = request.getParameter("id");
            if (!NumberUtils.isCreatable(idStr)) {
                response.getWriter().write(JSONObject.toJSONString(Message.failure("format error")));
                return;
            }
            int id = Integer.valueOf(idStr);
            String course=request.getParameter("course");
            Score s=sm.findNeedScore(id,course);
            response.getWriter().write(JSONObject.toJSONString(Message.success(s)));
    }
}
